package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.yiling.YiLingBattleHandle;

/****************************************
 * @name CMD_YiLingBonusReceive
 * @description 夷陵之战 通关奖励领取
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBonusReceive, classtype = YiLingMessage.YiLingBonusReceiveRequest.class)
public class CMD_YiLingBonusReceive extends Command<YiLingMessage.YiLingBonusReceiveRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBonusReceiveRequest request) {
        try {
            String playerCode = messageSource.getTag();
            int id = request.getId();
            int type = request.getType();
            YiLingMessage.YiLingBonusReceiveResponse.Builder response = YiLingMessage.YiLingBonusReceiveResponse.newBuilder();

            //type = 1 : 通关进度领取   type = 2 : 宝箱领取
            if (type != 1 && type != 2) {
                send(messageSource, response, Result.DataError);
                return;
            }

            Result result = null;
            if (type == 1) {
                result = new YiLingBattleHandle().goThroughReward(playerCode, id, response);
            }

            if (type == 2) {
                result = new YiLingBattleHandle().levelReward(playerCode, id, response);
            }

            send(messageSource, response, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBonusReceiveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}